#ifndef _SCUDB_GLOBAL_H_
#define _SCUDB_GLOBAL_H_


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <assert.h>
#include <errno.h>
#ifndef WIN32
#include <stdint.h>
#endif

#ifndef __cplusplus

#ifndef bool
typedef char bool;
#endif

#ifndef true
#define true	((bool) 1)
#endif

#ifndef false
#define false	((bool) 0)
#endif

#endif   /* __cplusplus */


#ifdef WIN32
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int32 uint32;
typedef unsigned __int64 uint64;
#include <malloc.h>
#else
typedef uint32_t uint32;
typedef uint64_t uint64;
#endif


#ifndef uint8
typedef char uint8;
#endif
typedef uint64 sd_uid;
#define SD_UID_ZERO(uid)  (uid=0)
#define SD_UID_NEXT(uid) ((uid)+1)
#define SD_UID_INC(uid)  ((uid)=SD_UID_NEXT(uid))
#define SD_UID_CMP(id1,id2) ((id1)==(id2))


#ifndef MIN
#define MIN(a,b) ((a)>(b)?(b):(a))
#endif

#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif


#define SD_MAX_PAYLOAD_LEN 100
#define SD_MAX_VARCHAR_LEN 128
typedef enum sd_KeyType
{
	SD_KEYTYPE_SHORT,
	SD_KEYTYPE_INT,
	SD_KEYTYPE_VARCHAR
}sd_KeyType;

typedef struct
{
	sd_KeyType type;
	union {
		int32_t shortkey;
		int64_t intkey;
		char charkey[SD_MAX_VARCHAR_LEN + 1];
	}keyval;
}sd_Key;

typedef struct
{
	sd_Key key;
	char payload[SD_MAX_PAYLOAD_LEN + 1];
	sd_uid rid;
}sd_Record;


/* Definition of return code */
typedef int sd_Ret;
#define SD_RET_OK               0
#define SD_RET_ERROR            -1
#define SD_RET_TRUE             1
#define SD_RET_FALSE            0

#define SD_RET_FILE_CANNOT_OPEN -2

#define SD_RET_KEY_NOTFOUND     -10
#define SD_RET_RECORD_NOTFOUND  -11
#define SD_SET_RECORD_EXISTS     -12
#define SD_RET_DATA_EMPTY       -13
#define SD_RET_DB_END           -14
#define SD_RET_DEAD_LOCK        -15


#define SD_RET_TRANSACTION_NO_AVAILABLE   -51
#define SD_RET_TRANSACTION_ERR_LOCK       -52

#define SD_RET_ALLOCATE_MEMEORY -100
#define SD_RET_LOCK_BUSY        -110



/* Definition of normal macros */
#ifdef _DEBUG
#define SD_ASSERT(ret) assert(ret)
#else
#define SD_ASSERT(ret) 
#endif

#define SD_FAILED(ret) (ret<0)

#define SD_ON_FAILED_RETURN(ret)  \
{									\
	if (SD_FAILED(ret))		\
	{	SD_ASSERT(false);				\
		return ret;					\
	}								\
};




/* Definition of the comparison function of two keys */
typedef int (*sd_KeyCompare)(const sd_Key* k1, const sd_Key* k2);
typedef int (*sd_KeyHashcode)(const sd_Key* key);

int sd_KeyShortCompare(const sd_Key* k1, const sd_Key* k2);
int sd_KeyIntCompare(const sd_Key* k1, const sd_Key* k2);
int sd_KeyCharCompare(const sd_Key* k1, const sd_Key* k2);
uint32 sd_KeyShortHashcode(const sd_Key* k);
uint32 sd_KeyIntHashcode(const sd_Key* k);
uint32 sd_KeyCharHashcode(const sd_Key* k);

void sd_KeyPrint(const sd_Key* k, FILE* fp);

#endif

